//
//  BHApplyPayBottomSheetView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/17.
//

import UIKit
protocol BHApplyPayBottomSheetViewDelegate {
    func goPayAct()
}

class BHApplyPayBottomSheetView: UIView {
    
    var delegate: BHApplyPayBottomSheetViewDelegate?
    
    
    lazy var contentBackView: UIView = {
        let view = UIView.init()
        view.backgroundColor = .white
        return view
    }()
    
    lazy var price_label: UILabel = {
        let label = UILabel.init()
        label.textColor = .color_333333
        label.font = .systemFont(ofSize: 16, weight: .medium)
        label.textAlignment = .center
        return label
    }()
    
    lazy var closeBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setImage(UIImage.init(named: "bh_com_vcClose_sign"), for: .normal)
        return btn
    }()
    
    lazy var name_label: UILabel = {
        let label = UILabel.init()
        label.text = "付款方式"
        label.font  = .systemFont(ofSize: 14, weight: .medium)
        label.textColor = .color_333333
        return label
    }()
    
    lazy var payway_label: UILabel = {
        let label = UILabel.init()
        label.text = "支付宝"
        label.textAlignment = .right
        label.font  = .systemFont(ofSize: 14, weight: .medium)
        label.textColor = .color_999999
        return label
    }()
    
    lazy var line: UILabel = {
        let label = UILabel.init()
        label.backgroundColor = .color_dbdbdb
        return label
    }()
    
    lazy var confirmBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.backgroundColor = .color_34CE98
        btn.setTitle("立即付款", for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .medium)
        btn.setTitleColor(.white, for: .normal)
        return btn
    }()
    
    
    fileprivate let height: CGFloat = 360.0
    
    var price: String = "0.00" {
        didSet {
            let appendPrice = "¥"+price
            
            let priceAttribute = appendPrice.toAttribute()
            priceAttribute.addFont(.systemFont(ofSize: 40, weight: .medium))
            priceAttribute.addAllTextColor(.color_333333)
            priceAttribute.addSpecialFont("¥", .systemFont(ofSize: 24, weight: .medium))
            self.price_label.attributedText = priceAttribute
        }
    }
    
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isHidden = true
        self.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        
        self.contentBackView.frame = CGRect(x: 0, y: UIScreen.height-UIScreen.safeBottomHeight-height, width: UIScreen.width, height: UIScreen.safeBottomHeight+height)
        
        self.closeBtn.frame = CGRect(x: 10, y: 10, width: 40, height: 40)
        self.price_label.frame = CGRect(x: 10, y: 55, width: UIScreen.width-20, height: 35)
        self.name_label.frame = CGRect(x: 16, y: 150, width: 60, height: 18)
        self.payway_label.frame = CGRect(x: 100, y: 150, width: UIScreen.width-116, height: 18)
        self.line.frame = CGRect(x: 16, y: 183, width: UIScreen.width-32, height: 0.5)
        self.confirmBtn.frame = CGRect(x: 16, y: (UIScreen.safeBottomHeight+height)-10-UIScreen.safeBottomHeight-44, width: UIScreen.width-32, height: 44)
        
        self.confirmBtn.addLayerCorner(4, 0, .white)

        self.addSubview(self.contentBackView)
        self.contentBackView.addSubview(self.closeBtn)
        self.contentBackView.addSubview(self.price_label)
        self.contentBackView.addSubview(self.name_label)
        self.contentBackView.addSubview(self.payway_label)
        self.contentBackView.addSubview(self.line)
        self.contentBackView.addSubview(self.confirmBtn)
        
        self.contentBackView.addCorner(conrners: [.topLeft,.topRight], radius: 12)

        self.contentBackView.transform = CGAffineTransform.init(translationX: 0, y: height)
        
        self.closeBtn.addBtnAction(action: {[weak self] (_) in
           
            self?.hide()
        }, for: .touchUpInside)
        
        self.confirmBtn.addBtnAction(action: {[weak self] (_) in
            self?.delegate?.goPayAct()
            self?.hide()
        }, for: .touchUpInside)
        

        
    }
    
    
    func show()  {
        self.isHidden = false
        UIView.animate(withDuration: 0.25) {
            self.contentBackView.transform = CGAffineTransform.identity
        }
    }
    
    func hide() {
        UIView.animate(withDuration: 0.25) {
            self.contentBackView.transform = CGAffineTransform.init(translationX: 0, y: self.height)
        } completion: { (finished) in
            self.isHidden = true
            
        }
    }
}
